package com.zc.trendsDispatch.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.zc.common.constant.RedisKeyConstants;
import com.zc.iotDevice.domain.ChargeStationPile;
import com.zc.iotDevice.domain.ChargeStationPort;
import com.zc.iotDevice.mapper.ChargeStationPileMapper;
import com.zc.iotDevice.mapper.ChargeStationPortMapper;
import com.zc.manager.device.entity.DeviceProperty;
import com.zc.operator.domain.ChargeStation;
import com.zc.operator.mapper.ChargeStationMapper;
import com.zc.remoteControl.service.RemoteInstructSendService;
import com.zc.trendsDispatch.domain.ChargeStationDispatch;
import com.zc.trendsDispatch.domain.ChargeStationRealPowerHis;
import com.zc.trendsDispatch.service.StationDispatchService;
import com.zc.util.ArithmeticUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import static com.ruoyi.common.utils.SecurityUtils.getDeptId;

/**
 * @Author glb
 * @Date 2023/12/13 10:58
 * @Version 1.0
 */
@Service
public class StationDispatchServiceImpl implements StationDispatchService {

    @Resource
    private RedisCache redisCache;

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Resource
    private ChargeStationMapper chargeStationMapper;

    @Resource
    private ChargeStationPileMapper chargeStationPileMapper;

    @Resource
    private ChargeStationPortMapper chargeStationPortMapper;

    @Resource
    private RemoteInstructSendService remoteInstructSendService;



    @Override
    public List<ChargeStation> getStationRealPower(ChargeStation chargeStation) {
        List<ChargeStation> chargeStations = chargeStationMapper.selectChargeStationList(chargeStation);
        Map<String, ChargeStationRealPowerHis> cacheMap = redisCache.getCacheMap(RedisKeyConstants.STATION_REAL_POWER);
        chargeStations.forEach(station->{
            ChargeStationRealPowerHis chargeStationRealPowerHis = cacheMap.get(station.getChargeStationCode());
            if(chargeStationRealPowerHis==null){
                station.setStationRealPower("-kw");
                station.setChargeStationRealPower("-kw");
                station.setChargeStationAllocationPower("-kw");
            }else{
                station.setStationRealPower(cacheMap.get(station.getChargeStationCode()).getStationRealPower());
                station.setChargeStationRealPower(cacheMap.get(station.getChargeStationCode()).getChargeStationRealPower());
                station.setChargeStationAllocationPower(cacheMap.get(station.getChargeStationCode()).getChargeStationAllocationPower());
                station.setUpdateTime(cacheMap.get(station.getChargeStationCode()).getUpdateTime());
            }
        });
        return chargeStations;
    }

    @Override
    public List<ChargeStationPile> getStationRealDetails(ChargeStationPile chargeStationPile) {
        List<ChargeStationPile> list = chargeStationPileMapper.getList(chargeStationPile);
        list.forEach(pile->{
            String deviceId = pile.getDeviceId();
            ChargeStationPort chargeStationPort=new ChargeStationPort();
            chargeStationPort.setDeviceId(deviceId);
            AtomicReference<Double> chargeStationRealPower= new AtomicReference<>((double) 0);
            List<ChargeStationPort> listPort = chargeStationPortMapper.getList(chargeStationPort);
            Map<String, DeviceProperty> realDataPile = remoteInstructSendService.getRealData(pile.getDeviceId());
            listPort.forEach(port->{
                Map<String, DeviceProperty> realData = remoteInstructSendService.getRealData(port.getId());
                String status="0";
                if(realData.containsKey("runningState")){
                    String AStatic = String.valueOf(realData.get("runningState").getValue());
                    if("1".equals(AStatic)){
                        status="3";
                    }else if("2".equals(AStatic)){
                        status="1";
                    }else if("3".equals(AStatic)){
                        status="2";
                        pile.setServiceStatus("2");
                    }
                }
                port.setServiceStatus(status);
                if(realData.containsKey("power")){
                    String power = String.valueOf(realData.get("power").getValue());
                    if(StringUtils.isNotBlank(power)){
                        double now= Double.parseDouble(power);
                        port.setRealPower(String.valueOf(now));
                        chargeStationRealPower.set(ArithmeticUtils.add(chargeStationRealPower.get(), now));
                    }else{
                        port.setRealPower("-");
                    }
                }else{
                    port.setRealPower("-");
                }
            });
            if(!"2".equals(pile.getServiceStatus())){
                if("online".equals(pile.getDeviceState())){
                    pile.setServiceStatus("1");
                }else {
                    pile.setServiceStatus("0");
                }
            }
            if(realDataPile.containsKey("procedureVersion")){
                pile.setProcedureVersion(String.valueOf(realDataPile.get("procedureVersion").getValue()));
            }
            if(realDataPile.containsKey("networkLinkType")){
                pile.setNetworkLinkType(String.valueOf(realDataPile.get("networkLinkType").getValue()));
            }
            if(realDataPile.containsKey("maxPower")){
                pile.setMaxPower(String.valueOf(realDataPile.get("maxPower").getValue()));
            }else{
                pile.setMaxPower("-");
            }
            pile.setRealPower(chargeStationRealPower.toString());
            pile.setChargePortList(listPort);
        });
        return list;
    }
}
